﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Windows.Media;
using System.IO;

namespace WoodRobotics.Weather
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }

    public class ImgSrcMultiBinding : IMultiValueConverter
    {

        #region IMultiValueConverter Members
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values[1].GetType() == typeof(string) && values[1].ToString() != "")
            {
                //Bitmap l_BM = BitmapImageToBitmap(new BitmapImage(new Uri(values[1].ToString(), UriKind.RelativeOrAbsolute)));

                return new BitmapImage(new Uri("http://www.google.com" + values[1].ToString(), UriKind.RelativeOrAbsolute));
            }
            else
                return null;
            //turn ;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion

        private Bitmap BitmapImageToBitmap(ImageSource Original)
        {

            if (Original == null)
                return (Bitmap)new Bitmap(1, 1);

            MemoryStream l_NewMS = new MemoryStream();
            BmpBitmapEncoder l_BBE = new BmpBitmapEncoder();
            l_BBE.Frames.Add(BitmapFrame.Create((BitmapSource)Original));
            l_BBE.Save(l_NewMS);
            l_NewMS.Seek(0, SeekOrigin.Begin);

            Bitmap l_ReturnBM = (Bitmap)Bitmap.FromStream(l_NewMS);

            l_ReturnBM.MakeTransparent(System.Drawing.Color.White);

            return l_ReturnBM;

        }
    }

    public class CombineStringsMultiBinding : IMultiValueConverter
    {
        #region IMultiValueConverter Members
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (values[0] == DependencyProperty.UnsetValue)
                return "";
            
            string l_Combined = "";

            foreach (object l_Str in values)
            {
                if (l_Str.GetType() != typeof(string))
                    return l_Combined;
                l_Combined += l_Str + "\r\n";
            }
            return l_Combined;
            
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }

}
